package com.server.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICaseService;
import com.server.service.ICourseManageService;
import com.server.service.IEstimateService;
import com.server.service.IForeignService;
import com.server.service.IPapersPackagingService;
import com.server.service.ISurveyPaperService;
import com.server.service.IUserAssignmentService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

/**
 * 评估
 * @author ServerZhang
 * @date 2016年7月27日
 */
/**
 * 
 * @author xiaoxiaoping
 * @date 2016年8月3日
 */
@Controller
@RequestMapping("estimate")
public class EstimateController {
	
	@Autowired
	private IEstimateService estimateService;
	@Autowired
	private ICourseManageService courseManageService;
	@Autowired
	private IUserAssignmentService userAssignmentService;
	@Autowired
	private IPapersPackagingService papersPackagingService;
	@Autowired
	private ISurveyPaperService surveyService;
	@Autowired
	private IForeignService foreignService;
	@Autowired
	private ICaseService caseService;

	 /**
	 * 课程评估
	 * @param request
	 * @param response
	 * @return
	 * @author ServerZhang
	 * @date 2016年8月1日
	 */
	@Login
	@RequestMapping("course")
	public ModelAndView course(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("estimate/courseList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	
	 /**
	 * 课程列表
	 * @param request
	 * @param response
	 * @param state
	 * @author ServerZhang
	 * @date 2016年7月27日
	 */
	@RequestMapping("courseList/{state}")
	public void courseList(HttpServletRequest request, HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> courseList = estimateService.courseList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, courseList.getCount(), JSONSerielizer.toJson(courseList.getList(), null, null));
	}
	 /**
	 * 课程评估
	 * @param request
	 * @param response
	 * @param state
	 * @param id
	 * @author ServerZhang
	 * @date 2016年7月29日
	 */
	@Login
	@RequestMapping("courseEs/{state}/{id}")
	public void courseEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存评估信息
		isSuccess = estimateService.add(id, 1, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
			isSuccess = userAssignmentService.checkAssi(id, 1, 1);
		}
		//3、修改评估状态
		if (isSuccess) {
			isSuccess = courseManageService.updateState(id.toString(), state == 1 ? 3 : 2, operater,false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	 * 
	 * 案例评估列表
	 * @return
	 * @author xiaoxiaoping
	 * @date 2016年8月2日
	 */
	@Login
	@RequestMapping("case")
	public ModelAndView casePage(){
		ModelAndView mv = new ModelAndView("estimate/caseList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("caseList/{state}")
	public void caseList(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> caseList = estimateService.caseList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, caseList.getCount(), JSONSerielizer.toJson(caseList.getList(), null, null));
	}
	@Login
	@RequestMapping("caseEs/{state}/{id}")
	public void caseEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存评估信息
		isSuccess = estimateService.add(id, 3, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
			isSuccess = userAssignmentService.checkAssi(id, 3, 1);
		}
		//3、修改评估状态
		if (isSuccess) {
			isSuccess = caseService.updateState(id.toString(), state == 1 ? 3 : 2, operater,false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	 * 试卷评估
	 * 
	 * @return
	 * @author xiaoxiaoping
	 * @date 2016年8月3日
	 */
	@Login
	@RequestMapping("exam")
	public ModelAndView examPage(){
		ModelAndView mv = new ModelAndView("estimate/examList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("examList/{state}")
	public void examList(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> examList = estimateService.examList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, examList.getCount(), JSONSerielizer.toJson(examList.getList(), null, null));
	}
	@Login
	@RequestMapping("examEs/{state}/{id}")
	public void examEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存评估信息
		isSuccess = estimateService.add(id, 2, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
			isSuccess = userAssignmentService.checkAssi(id, 2, 1);
		}
		//3、修改评估状态
		if (isSuccess) {
			isSuccess = papersPackagingService.updateState(id.toString(), state == 1 ? 3 : 2, operater,false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	 * 
	 * 调研试题评估
	 * @return
	 * @author xiaoxiaoping
	 * @date 2016年8月3日
	 */
	@Login
	@RequestMapping("survey")
	public ModelAndView surveyPage(){
		ModelAndView mv = new ModelAndView("estimate/surveyList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("surveyList/{state}")
	public void surveyList(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> examList = estimateService.surveyList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, examList.getCount(), JSONSerielizer.toJson(examList.getList(), null, null));
	}
	@Login
	@RequestMapping("surveyEs/{state}/{id}")
	public void surveyEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存评估信息
		isSuccess = estimateService.add(id, 4, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
			isSuccess = userAssignmentService.checkAssi(id, 4, 1);
		}
		//3、修改评估状态
		if (isSuccess) {
			isSuccess = surveyService.updateState(id.toString(), state == 1 ? 3 : 2, operater,false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	 * 涉外资料审核-国别/地区
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @author xiaoxiaoping
	 * @date 2016年8月4日
	 */
	@Login
	@RequestMapping("sicfCon")
	public ModelAndView sicfConPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("estimate/sicfConList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@Login
	@RequestMapping("sicfConList/{state}")
	public void sicfConList(HttpServletRequest request, HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> examList = estimateService.sicfConList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, examList.getCount(), JSONSerielizer.toJson(examList.getList(), null, null));
	}
	@Login
	@RequestMapping("sicfConEs/{state}/{id}")
	public void sicfConEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存审核信息
		isSuccess = estimateService.add(id, 5, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
				isSuccess = userAssignmentService.checkAssi(id, 5, 1);
		}
		//2、修改审核状态
		if (isSuccess) {
			isSuccess = foreignService.updateState(id.toString(), state == 1 ? 3 : 2, operater,"1",false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	 * 涉外
	 */
	@Login
	@RequestMapping("sicfFor")
	public ModelAndView sicfForPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("estimate/sicfForList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@Login
	@RequestMapping("sicfForList/{state}")
	public void sicfForList(HttpServletRequest request, HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> examList = estimateService.sicfForList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, examList.getCount(), JSONSerielizer.toJson(examList.getList(), null, null));
	}
	@Login
	@RequestMapping("sicfForEs/{state}/{id}")
	public void sicfForEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存审核信息
		isSuccess = estimateService.add(id, 6, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
			isSuccess = userAssignmentService.checkAssi(id, 6, 1);
		}	
		//3、修改审核状态
		if (isSuccess) {
			isSuccess = foreignService.updateState(id.toString(), state == 1 ? 3 : 2, operater,"2",false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	 * 应急处理
	 */
	@Login
	@RequestMapping("sicfEmr")
	public ModelAndView sicfEmrPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("estimate/sicfEmrList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@Login
	@RequestMapping("sicfEmrList/{state}")
	public void sicfEmrList(HttpServletRequest request, HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> examList = estimateService.sicfEmrList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, examList.getCount(), JSONSerielizer.toJson(examList.getList(), null, null));
	}
	@Login
	@RequestMapping("sicfEmrEs/{state}/{id}")
	public void sicfEmrEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存评估信息
		isSuccess = estimateService.add(id, 7, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
				isSuccess = userAssignmentService.checkAssi(id, 7, 1);
		}
		//3、修改审核状态
		if (isSuccess) {
			isSuccess = foreignService.updateState(id.toString(), state == 1 ? 3 : 2, operater,"3",false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	 * 相关专题
	 */
	@Login
	@RequestMapping("sicfRel")
	public ModelAndView sicfRalPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("estimate/sicfRelList");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@Login
	@RequestMapping("sicfRelList/{state}")
	public void sicfRalList(HttpServletRequest request, HttpServletResponse response,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		DataVector<Map<String, Object>> examList = estimateService.sicfRelList(userName, state, new RequestInput(request));
		OutputUtil.returnGridData(response, examList.getCount(), JSONSerielizer.toJson(examList.getList(), null, null));
	}
	@Login
	@RequestMapping("sicfRelEs/{state}/{id}")
	public void sicfRalEs(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer state, 
			@PathVariable Integer id){
		Boolean isSuccess = true;
		String operater = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String content = request.getParameter("content");
		//1、保存审核信息
		isSuccess = estimateService.add(id, 8, state, content, operater);
		//2、评估通过系统分配审核人员
		if (isSuccess && state == 1) {
			isSuccess = userAssignmentService.checkAssi(id, 8, 1);
		}
		//3、修改审核状态
		if (isSuccess) {
			isSuccess = foreignService.updateState(id.toString(), state == 1 ? 3 : 2, operater,"4",false);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	@Login
	@RequestMapping("getEvalue/{id}/{typeId}")
	public void getEvalue(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer id,@PathVariable Integer typeId){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		List<Map<String, Object>> list = estimateService.getEvalue(operator, id, typeId);
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
}
